(function(window, QrCodeRecognition) {
	'use strict';

	if (typeof define === 'function' && define.amd) {
		define(QrCodeRecognition());
	} else if (typeof exports === 'object') {
		module.exports = QrCodeRecognition();
	} else {
		window.QrCodeRecognition = QrCodeRecognition();
	}
})(typeof window !== 'undefined' ? window : this, () => {
	'use strict';
	return class QrCodeRecognition {
		constructor(opts = {}) {
			this.timer = null;
			this.result = '';
			this.isAnimation = true;
			this.lineWidth = opts.borderWidth || 3;
			this.strokeStyle = opts.lineColor || 'red';
			this.success = opts.success || Function;
			this.error = opts.error || Function;
		}

		draw(begin, end) {
			this.canvas.beginPath();
			this.canvas.moveTo(begin.x, begin.y);
			this.canvas.lineTo(end.x, end.y);
			this.canvas.lineWidth = this.lineWidth;
			this.canvas.strokeStyle = this.strokeStyle;
			this.canvas.stroke();
		}

		cance() {
			this.isAnimation = false;
			cancelAnimationFrame(this.timer);
		}

		upload(imgBase) {
			Jimp.read(imgBase)
				.then(async res => {
					const { data, width, height } = res.bitmap;
					try {
						const resolve = await jsQR(data, width, height);
						this.success(resolve);
					} catch (err) {
						this.error(err);
					}
				})
				.catch(err => {
					this.error(err);
				});
		}
	};
});
